package cn.com.headfree.util;

import cn.com.headfree.generalquery.vo.GeneralManageQueryVo;
import org.apache.log4j.Logger;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.xssf.usermodel.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.List;

public class GeneralManageQueryExportExcelUtil {

    private static Logger log = Logger.getLogger(ExcelExportUtil.class);

    private GeneralManageQueryExportExcelUtil() {
    }

    /**
     * 生成sheet并导入内容 传入工作簿，创建sheet
     * @param workBook
     * @param headers
     * @param content
     * @param sheetName
     * @return
     */
    public static XSSFWorkbook fillDataToExcel(XSSFWorkbook workBook, String[] headers, String[][] content, String sheetName) {

        XSSFCellStyle cellStyle = workBook.createCellStyle();
        //设置标题单元格样式
        XSSFCellStyle cellStyleHeader = workBook.createCellStyle();
        cellStyleHeader.setAlignment(HorizontalAlignment.CENTER);
        cellStyleHeader.setVerticalAlignment(VerticalAlignment.CENTER);

        cellStyleHeader.setBorderBottom(BorderStyle.THIN); // 下边框
        cellStyleHeader.setBorderLeft(BorderStyle.THIN);// 左边框
        cellStyleHeader.setBorderTop(BorderStyle.THIN);// 上边框
        cellStyleHeader.setBorderRight(BorderStyle.THIN);// 右边框
        cellStyleHeader.setFillForegroundColor(new XSSFColor(new java.awt.Color(156, 195, 230)));//设置背景色
        cellStyleHeader.setFillPattern(FillPatternType.ALT_BARS);//填充模式
        //水平居中
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        //垂直居中
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        cellStyle.setBorderBottom(BorderStyle.THIN); // 下边框
        cellStyle.setBorderLeft(BorderStyle.THIN);// 左边框
        cellStyle.setBorderTop(BorderStyle.THIN);// 上边框
        cellStyle.setBorderRight(BorderStyle.THIN);// 右边框

        XSSFSheet sheet = workBook.createSheet(sheetName);
        sheet.setVerticallyCenter(true);
        XSSFRow row = sheet.createRow(0);
        for (int i = 0; i < headers.length; i++) {
            XSSFCell cell = row.createCell(i);
            if(i == 1){
                sheet.setColumnWidth(i, 90 * 256);//设置宽度为90字节
            }else if(i == 0 || i == 2 || i == 3){
                sheet.setColumnWidth(i, 10 * 256);//设置宽度为10字节
            }else if(i == 8){
                sheet.setColumnWidth(i, 50 * 256);//设置宽度为50字节
            }else{
                sheet.setColumnWidth(i, 15 * 256);//设置宽度为15字节
            }
            cell.setCellValue(headers[i]);
            cell.setCellStyle(cellStyleHeader);
        }
        if(content != null){
            for (int i = 0; i < content.length; i++) {
                row = sheet.createRow((int) i + 1);//从第二行开始添加内容
                //写入数据
                if(content != null){
                    for (int j = 0; j < content[i].length; j++) {
                        XSSFCell cell2 = row.createCell(j);//在第一列创建表格
                        cell2.setCellValue(content[i][j]);
                        cell2.setCellStyle(cellStyle);
                    }
                }
            }
        }
        return workBook;
    }

    /**
     * 组装数据
     * @return generalManageQueryTitle=姓名,部门,年龄,工龄,入职时间,学历,职称名称,技能等级,人才称号
     */
    public static String[][] makeData(List<GeneralManageQueryVo> list, String[] titles) {
        String[][] content = null;
        content = new String[list.size()][titles.length];
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                content[i][0] = list.get(i).getName();
                content[i][1] = list.get(i).getDeptName();
                if(list.get(i).getAge() != 0){
                    content[i][2] = list.get(i).getAge()+"";
                }
                if(list.get(i).getEmploylength() != 0){
                    content[i][3] = list.get(i).getEmploylength()+"";
                }
                if(list.get(i).getJobDate() != null){
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
                    content[i][4] = sdf.format(list.get(i).getJobDate());
                }
                content[i][5] = list.get(i).getEducationName();
                content[i][6] = list.get(i).getSubjlevelname();
                content[i][7] = list.get(i).getQualifilevelname();
                content[i][8] = list.get(i).getEmpTitleName();
            }
        }
        return content;
    }

    public static void setResponseHeader(HttpServletRequest request,HttpServletResponse response, String fileName) {
        try {
            // 解决文件乱码
            fileName = URLEncoder.encode(fileName, "UTF8").replaceAll("\\+", "%20");// 其他浏览器
            response.setContentType("application/vnd.ms-excel;charset=UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
            response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
        } catch (Exception e) {
//            log.info("****************4");
            e.printStackTrace();
        }
    }

}
